package com.ymatou.iapi.csim.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.OnlineBean;
import com.ymatou.iapi.csim.parameter.OpenSessionBean;
import com.ymatou.iapi.csim.service.OnlineCall;
import com.ymatou.iapi.csim.service.OpenSessionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
/**
*客服主动开启新会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OpenSession{
	private static OpenSessionBean bean;
	private static OpenSessionCall call;
	private CsimCallService service=new CsimCallService();
	private int csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private Integer customerId=Integer.parseInt(EnvSetup.getData("customerId").toString()); 
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服主动开启新会话");
	}

	@Before
	public void caseUp() {
		bean=new OpenSessionBean();
		call=new OpenSessionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenSession_001() {
		Logger.start(true,"客服主动开启新会话");
		try {
			service.getOnlineCS(csId);
			ymt_cssession cssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			if(cssession.getId()!=null)
			{
				csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId));
			}
			bean.setCsId(csId);
			bean.setToUserId(userId);
			bean.setMessage("客服主动开启新会话_自动化测试_"+TestDataManager.getRandomIntNum(5));
			
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("true", call.getSuccess(), "验证success");
			ymt_cssession cssession2=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			Logger.verifyEquals(cssession2.getId(), call.getSeesionId(), "验证SeesionId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenSession_002() {
		Logger.start(true,"客服主动开启新会话-会话存在");
		try {
			service.getOnlineCS(csId);
			bean.setCsId(csId);
			bean.setToUserId(userId);
			bean.setMessage("客服主动开启新会话_自动化测试_"+TestDataManager.getRandomIntNum(5));
			
			call.setData(bean);
			call.callService();		
			call.callService();	
			
			Logger.verifyEquals("false", call.getSuccess(), "验证success");
			Logger.verifyEquals("", call.getSeesionId(), "验证SeesionId");
			Logger.verifyEquals(true, call.getErrorMessage().contains("客服主动开启会话失败，用户已在会话中"), "验证ErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenSession_003() {
		Logger.start(true,"客服主动开启新会话-客服不在线");
		try {
			//service.getOnlineCS(csId);
			onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",csId));
			ymt_cssession cssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			if(cssession.getId()!=null)
			{
				csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId));
			}
			bean.setCsId(csId);
			bean.setToUserId(userId);
			bean.setMessage("客服主动开启新会话_自动化测试_"+TestDataManager.getRandomIntNum(5));
			
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("false", call.getSuccess(), "验证success");
			Logger.verifyEquals("", call.getSeesionId(), "验证SeesionId");
			Logger.verifyEquals(true, call.getErrorMessage().contains("未找到对应客服"), "验证ErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}